package com.dankegongyu.lib.common.base;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.dankegongyu.lib.common.annotation.BindEventBus;
import com.dankegongyu.lib.common.event.DKEventBus;
import com.dankegongyu.lib.common.rx.presenter.IPresenter;
import com.dankegongyu.lib.common.util.fresco.FrescoImageHelper;
import com.trello.rxlifecycle3.components.support.RxFragment;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * @author WuPuquan
 * @version 1.0
 * @since 2017/9/14 10:21
 */

public abstract class CommonBaseFragment extends RxFragment implements IBaseFragment {

    private Unbinder unbinder;

    protected List<IPresenter> mPresenterList = new ArrayList<>();

    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
//        Logger.d("onAttach");
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        Logger.d("onCreate");
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
//        Logger.d("onCreateView");
        if (getContentViewLayoutId() != 0) {
            View rootView = inflater.inflate(getContentViewLayoutId(), container, false);
            unbinder = ButterKnife.bind(this, rootView);
            if (this.getClass().isAnnotationPresent(BindEventBus.class)) {
                DKEventBus.register(this);
            }
            return rootView;
        } else {
            return super.onCreateView(inflater, container, savedInstanceState);
        }
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
//        Logger.d("onActivityCreated");

        init(savedInstanceState);

        // Presenter
        initPresenter();
        for (IPresenter p : mPresenterList) {
            if (p != null) {
                p.onCreate();
            }
        }

        doBusiness();
    }

    @Override
    public void onStart() {
        super.onStart();
//        Logger.d("onStart");
    }

    @Override
    public void onResume() {
        super.onResume();
//        Logger.d("onResume");
    }

    @Override
    public void onPause() {
        super.onPause();
//        Logger.d("onPause");
    }

    @Override
    public void onStop() {
        super.onStop();
//        Logger.d("onStop");
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
//        Logger.d("onDestroyView");

        // 1.EventBus解注册
        if (this.getClass().isAnnotationPresent(BindEventBus.class)) {
            DKEventBus.unregister(this);
        }

        // 2.Presenter销毁
        for (IPresenter p : mPresenterList) {
            if (p != null) {
                p.onDestroy();
            }
        }

        // 3.ButterKnife取消绑定，必须在Presenter销毁之后，否则容易造成空指针
        if (unbinder != null) {
            unbinder.unbind();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
//        Logger.d("onDestroy");
    }

    @Override
    public void onDetach() {
        super.onDetach();
//        Logger.d("onDetach");
    }

    @Override
    public void onLowMemory() {
        super.onLowMemory();
        FrescoImageHelper.clearMemoryCaches();
    }

    protected void addPresenter(IPresenter presenter) {
        if (presenter != null) {
            mPresenterList.add(presenter);
        }
    }

    protected void finishActivity() {
        if (getActivity() != null) {
            getActivity().finish();
        }
    }
}
